﻿using System;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ADministration.Web;
using System.Windows.Navigation;
using ADministration.Commands;

namespace ADministration.ViewModels
{
    public class AddCategoryViewModel
    {
        private NavigationService navigationService;
        private TicketsDomainContext context;

        public Category Category { get; set; }
        public ICommand AddCategoryCommand { get; set; }
        public ICommand CancelCategoryCommand { get; set; }

        public AddCategoryViewModel(NavigationService navigationService)
        {
            this.navigationService = navigationService;
            this.context = new TicketsDomainContext();

            this.Category = new Category();
            this.AddCategoryCommand = new RelayCommand(OnAddCategory);
            this.CancelCategoryCommand = new RelayCommand(OnCancel);
        }


        private void OnCancel()
        {
            this.navigationService.GoBack();
        }

        private void OnAddCategory()
        {
            if (!this.Category.HasValidationErrors)
            {
                context.RejectChanges();
                context.Categories.Add(this.Category);
                context.SubmitChanges(OnAddCategoryComplited,null);
            }
        }
  
        private void OnAddCategoryComplited(SubmitOperation operation)
        {
            if (!operation.HasError)
            {
                this.navigationService.GoBack();
            }
        }

        
    }
}
